import { createRouter, createWebHistory } from 'vue-router'
import LoginView from '../views/LoginView.vue'
import { ElMessage } from "element-plus";

const routes = [
  {
    path: '/',
    name: 'login',
    component: LoginView
  },
  {
    path: '/communitypersonnel',
    name: 'communitypersonnel',
    component: () => import( '../views/CommunityPersonnelView.vue')
  },
  {
    path: '/personneladd',
    name: 'personneladd',
    component: () => import( '../views/PersonnelAddView.vue')
  },
  {
    path: '/filing',
    name: 'filing',
    component: () => import( '../views/FilingView.vue')
  },
  {
    path: '/filingadd',
    name: 'filingadd',
    component: () => import( '../views/FilingAddView.vue')
  },
  {
    path: '/filingedit',
    name: 'filingedit',
    component: () => import( '../views/FilingEditView.vue')
  },
  {
    path: '/material',
    name: 'material',
    component: () => import( '../views/MaterialView.vue')
  },
  {
    path: '/notice',
    name: 'notice',
    component: () => import( '../views/NoticeView.vue')
  },
  {
    path: '/changepassword',
    name: 'changepassword',
    component: () => import( '../views/ChangePasswordView.vue')
  },
  {
    path: '/staffinfo',
    name: 'staffinfo',
    component: () => import( '../views/StaffInfoView.vue')
  },
  {
    path: '/noticeadd',
    name: 'noticeadd',
    component: () => import( '../views/NoticeAddView.vue')
  },
  {
    path: '/noticeedit',
    name: 'noticeedit',
    component: () => import( '../views/NoticeEditView.vue')
  },
  {
    path: '/notaccess',
    name: 'notaccess',
    component: () => import( '../views/NotAccessView.vue')
  }
]

const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes
})


router.beforeEach((to, from,  next) => {
  if(to.path === '/'||to.path === '/notaccess') return next(); //直接放行
  //验证身份
  let loginUser = sessionStorage.getItem('staff');
  if(!loginUser){
    ElMessage({type: 'error', message: "无权限，请先登录使用"});
    return next('/');
  }
  next();
})
export default router
